package com.yuyu.controller;

import com.sun.scenario.effect.impl.ImagePool;
import com.yuyu.bean.Employee;
import com.yuyu.exception.ExportExcelException;
import com.yuyu.exception.ImportExcelException;
import com.yuyu.service.EmployeeService;
import com.yuyu.uitlbean.PageData;
import com.yuyu.uitlbean.ResultObj;
import com.yuyu.uitlbean.ResultPageData;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    //跳转页面到员工资料管理页面
    @RequestMapping("/employee_manager.page")
    public String toEmployeeManager(){
        return "employee_manager";
    }

    //跳转到修改员工资料页面
    @RequestMapping("/edit_employee.page")
    public String toEditEmployee(){
        return "edit_employee";
    }

    //跳转到添加员工资料页面
    @RequestMapping("/add_employee.page")
    public String toAddEmployee() {return "add_employee"; }

    //跳转到上传员工excel表页面
    @RequestMapping("/upload_employee.page")
    public String toEmployeeUpload(){return "upload_employee"; }

    //过滤查找员工资料
    @RequestMapping("/filtrateEmployee")
    @ResponseBody
    public ResultObj<ResultPageData<List<Employee>>> filtrateEmployee(PageData page_data, String condition){
        return employeeService.filtrateEmployee(page_data, condition);
    }

    //通过登录名查找员工资料
    @RequestMapping("/getEmployeeByUserName")
    @ResponseBody
    public ResultObj<Employee> getEmployeeByUserName(String username){
        return employeeService.getEmployeeByUserName(username);
    }

    //编辑员工资料
    @RequestMapping("/editEmployee")
    @ResponseBody
    public ResultObj editEmployee(Employee employee){
        return employeeService.editEmployee(employee);
    }

    //删除员工资料
    @RequestMapping("/deleteEmployee")
    @ResponseBody
    public ResultObj deleteEmployee(@RequestBody List<Integer> list){
        return employeeService.deleteEmployeeById(list);
    }

    //添加员工资料
    @RequestMapping("/addEmployee")
    @ResponseBody
    public ResultObj addEmployee(Employee employee){
        return employeeService.addEmployee(employee);
    }

    //通过编码查找员工资料
    @RequestMapping("/getEmployeeById")
    @ResponseBody
    public ResultObj getEmployeeById(Integer id){
        return employeeService.getEmployeeById(id);
    }

    //导出EXCEL
    @RequestMapping("/exportEmpoloyeeExcel")
    @ResponseBody
    public void exportEmpoloyeeExcel(HttpServletRequest request, HttpServletResponse response){
        response.reset(); //清除buffer缓存
        //Map<String,Object> map=new HashMap<String,Object>();
        // 指定下载的文件名
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        try {
            response.setHeader("Content-Disposition","attachment;filename="+new String("用户表.xlsx".getBytes(),"iso-8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new ExportExcelException("导出失败");
        }
        //导出Excel对象
        XSSFWorkbook workbook = employeeService.exportEmployeeExcel();
        OutputStream output;
        try {
            output = response.getOutputStream();
            BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);
            bufferedOutput.flush();
            workbook.write(bufferedOutput);
            bufferedOutput.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new ExportExcelException("导出失败");
        }
    }

    //导入EXCEL

    @RequestMapping(value = "/importEmployeeExcel" ,method = RequestMethod.POST)
    @ResponseBody
    public ResultObj importEmployeeExcel(MultipartFile file){
        if(!(file.getOriginalFilename().endsWith("xls") || file.getOriginalFilename().endsWith("XLS")
                || file.getOriginalFilename().endsWith("xlsx")|| file.getOriginalFilename().endsWith("XLSX")
        )){
            throw new ImportExcelException("文件类型不合法");
        }
        if(file.isEmpty()){
            throw new ImportExcelException("文件不能为空");
        }

        XSSFSheet sheet = null;
        try {
            InputStream is = file.getInputStream();
            XSSFWorkbook hssf = new XSSFWorkbook(is);
            sheet = hssf.getSheetAt(0);
        } catch (IOException e) {
            throw new ImportExcelException("出错啦");
        }
        if(sheet == null){
            throw new ImportExcelException("出错啦");
        }



        return employeeService.importEmployeeExcel(sheet);
    }
}
